package org.dyh.lambdas.alternative;

import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;

/*
@Author=Joey Wheeler
@Date=2021/4/13 15:12
*/
public class ConsumerEnhancer {

    public static <T> Consumer<T> intercept(Consumer<T> moto, Predicate<T> filter){
        Objects.requireNonNull(moto);
        Objects.requireNonNull(filter);
        return t -> {
            if (filter.test(t))
                moto.accept(t);
        };
    }
    public static <T> Consumer<T> whenException(Consumer<T> moto, BiConsumer<Exception,T> exConsumer){
        Objects.requireNonNull(moto);
        Objects.requireNonNull(exConsumer);
        return t->{
            try {
                moto.accept(t);
            } catch (Exception ex) {
                exConsumer.accept(ex,t);
            }
        };
    }
}
